package com.tensquare.base.controller;

import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.xml.namespace.QName;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/label")
@RefreshScope
public class BaseController {

    @Autowired
    private LabelService labelService;

    @Value("${ip}")
    private String ip;

    @GetMapping
    public Result findAll(){
        System.out.println("ip==  "+ ip);
        return new Result(true, StatusCode.OK,"查询成功",labelService.findAll());

    }

    @GetMapping("/{labelId}")
    public Result findById(@PathVariable("labelId") String labelId ){
        return new Result(true, StatusCode.OK,"I查询成功",labelService.findById(labelId));
    }

    @PostMapping()
    public Result save(@RequestBody Label label){
        labelService.save(label);
        labelService.findBydate(label.getDate());
        return new Result(true, StatusCode.OK,"新增成功");
    }

    @PutMapping("/{labelId}")
    public Result update(@PathVariable String labelId,@RequestBody Label label){
        label.setId(labelId);
        labelService.update(label);
        return new Result(true, StatusCode.OK,"更新成功");
    }

    @DeleteMapping("/{labelId}")
    public Result delete(@PathVariable String labelId){
        labelService.delete(labelId);
        return new Result(true, StatusCode.OK,"删除成功");
    }

    @PostMapping("/search")
    public Result findSearch(@RequestBody Label label){
        List<Label> labelList=labelService.findSearch(label);
        return new Result(true, StatusCode.OK,"条件查询成功",labelList);
    }

    @PostMapping("/search/{page}/{size}")
    public Result pageQuery(@RequestBody Label label,@PathVariable int page,@PathVariable int size){
        Page<Label> pageDate=labelService.pageQuery(label,page,size);
        return new Result(true, StatusCode.OK,"条件查询成功",new PageResult<Label>(
                pageDate.getTotalElements(),
                pageDate.getContent()
        ));
    }


}
